package com.rdc.jiajiadictionary.data.getdailysentence;


import android.graphics.drawable.Drawable;

import com.rdc.jiajiadictionary.bean.DailySentence;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URL;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class DailySentenceModel implements DailySentenceModelInterface{

    private final String API_URl = "http://open.iciba.com/dsapi";
    @Override
    public void getSentenceFromApi(GetSentenceCallBack callBack) {
        get(API_URl,callBack);
    }

    /**
     * 进行网络请求，通过接口获得每日一句内容，并进行接口回调返回数据到presenter层
     * @param url
     * @param callBack
     */
    public void get(String url,GetSentenceCallBack callBack) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                callBack.onDataNotAvailable();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if(response.isSuccessful()){

                    String result = response.body().string();
                    DailySentence mDailySentence = parseJSONWithJSONObject(result);
                    callBack.onComplete(mDailySentence);
                }

            }
        });

//            }
//        }).start();
    }


    /**
     * 解析获取到的每日一句json
     * @param result
     * @return
     */
    private DailySentence parseJSONWithJSONObject(String result) {

        DailySentence dailySentence = new DailySentence();
        try {
            JSONArray jsonArray = new JSONArray(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(result);
            dailySentence.setEnglish(jsonObject.getString("content"));
            dailySentence.setChinese(jsonObject.getString("note"));
            dailySentence.setAudioUrl(jsonObject.getString("tts"));
            dailySentence.setDrawable(getPhotoFromUrl(jsonObject.getString("picture4")));
            dailySentence.setFullDrawable(getPhotoFromUrl(jsonObject.getString("fenxiang_img")));


        } catch (JSONException jsonException) {
            jsonException.printStackTrace();
        }
        return dailySentence;
    }

    /**
     * 通过url网址获取到drawable格式的图片
     * @param url
     * @return
     */
    public static Drawable getPhotoFromUrl(String url){
        Drawable drawable = null;
        try {
            drawable = Drawable.createFromStream(new URL(url).openStream(), "image.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return drawable;
    }

    /**
     * 获取数据回调接口
     */
    public interface GetSentenceCallBack {
        void onComplete(DailySentence dailySentence);
        void onDataNotAvailable();
    }
}
